package com.longg.service.impl;

import com.longg.dao.ConJoinDao;
import com.longg.dao.DeptDao;
import com.longg.dao.MeetingDao;
import com.longg.model.ConJoin;
import com.longg.model.Conference;
import com.longg.model.Dept;
import com.longg.service.IMeetingService;
import com.longg.utils.DateUtil;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class MeetingServiceImpl implements IMeetingService {
    private MeetingDao meetingDao = new MeetingDao();
    private DeptDao deptDao = new DeptDao();
    private ConJoinDao conJoinDao = new ConJoinDao();

    @Override
    public List<Map<String, Object>> findMeetingsByPage(int currentPage, String name,String deptName) {
        return meetingDao.findMeetingsByPage(currentPage,name,deptName);
    }

    @Override
    public long getCount(String name,String deptName) {
        return meetingDao.getCount(name,deptName);
    }

    @Override
    public int addMeeting(String title, String deptName, String startTime, String content, int uid) {
        List<Dept> deptList = deptDao.findByCol("name", deptName);
        Conference conference = new Conference();
        try {
            conference.setContent(content);
            conference.setDeptId(deptList.get(0).getId());
            conference.setDeptName(deptName);
            conference.setPublishDate(new Date());
            conference.setStartTime(DateUtil.stringToDate(startTime));
            conference.setTitle(title);
            conference.setContent(content);
            conference.setStatus(0);
            conference.setUserId(uid);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int save = (int) meetingDao.saveRId(conference);
        return save;
    }

    @Override
    public void joinMeeting(int cid, String deptName) {
        List<Dept> deptList = deptDao.findByCol("name", deptName);
        int did = deptList.get(0).getId();
        List<Map<String,Object>> uidsList = deptDao.getIdsById(did);
        for (Map<String,Object> map : uidsList){
            ConJoin conJoin = new ConJoin();
            conJoin.setCId(cid);
            conJoin.setUId((Integer) map.get("id"));
            conJoin.setStatus(0);
            conJoinDao.save(conJoin);
        }
    }

    @Override
    public Map<String, Object> meetDetail(int cid) {
        Map<String,Object> map = meetingDao.findMeetDetail(cid);
        return map;
    }
}
